#include "atoms.h"
#include <graphics.h>
#include <iostream.h>
#include <math.h>

const int hei=500, wid=900; //Size of window
const int AtomNumb = 30;
const int AIR = 10; //Atoms in row
const double v0 = 0.000000003;
const double drawdelay = 0.03;
const int aleft = 3;
const int aright = 1;
const int zerocoun = 100;

int main()
{
    int i, j, physcoun, coun;
    double oldv;
    int flag;
    double param;
    Atom myatoms [AtomNumb];

    initwindow (wid, hei);
    
//    param = sigma / 90 * 100;
    param = sigma * pow (2, 0.16666666);
    
    for (i=0;i<AtomNumb;i++) {
        myatoms[i].x = (i%AIR)*param + ((i-(i%AIR))/AIR%2)*param/2;      
        myatoms[i].y = (i-(i%AIR))/AIR*param*0.866;
    }    

    coun = 0;
    flag = 0;
    zerocoun = 2;    
    while (!flag) {
      coun++;    

      //Drawing atoms/
      for (i=0;i<AtomNumb;i++)
          myatoms[i].Draw();
            
      if (coun == zerocoun) {
          myatoms[(AtomNumb / AIR / 2) * AIR + aleft].vx = v0;
          myatoms[(AtomNumb / AIR / 2) * AIR + aleft].vy = 0.0;          
          cout << "Started";          
        }      
  
      for (physcoun=0;((physcoun<(drawdelay/dt))&&(!flag));physcoun++) {
        oldv = myatoms [(AtomNumb / AIR / 2) * AIR + AIR - 1].GiveV();
        for (i=0;i<AtomNumb;i++)
          for (j=i+1;j<AtomNumb;j++)
            myatoms [i].Interaction (&myatoms [j]);

        for (i=0;i<AtomNumb;i++)
          myatoms [i].Move();

        if (coun < zerocoun)         
          for (i=0;i<AtomNumb;i++) {
            myatoms [i].vx = 0;
            myatoms [i].vy = 0;
          }    
   
        if ((myatoms [(AtomNumb / AIR / 2) * AIR + AIR - aright].GiveV() < oldv) &&
            (myatoms [(AtomNumb / AIR / 2) * AIR + AIR - aright].GiveV() > v0/10) && (coun >= zerocoun)) {
          cout << "Yes";
          cout << "\n";
          cout << ((coun - zerocoun) * drawdelay) + dt * physcoun;
          cout << "\n";
          cout << param * (AIR - aleft - aright) / (((coun - zerocoun) * drawdelay) + dt * physcoun);
          flag = 1;                 
        }               
      }    
      delay (drawdelay * 1000);
      cleardevice ();
    }
        
    getch ();
    closegraph ();
    return 0;
}
